/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.cli.propbased;

import java.util.Iterator;
import java.util.Properties;

import gstruct.cli.CLIArgedOption;
import gstruct.cli.ParseResult;

/**
 * CLIArgedOption implementation that stores its results
 * in a {@link Properties} object.
 * @author Giulio Franco <giulio_franco@msn.com>
 */
public class CLIPropArgedOption extends CLIArgedOption<Properties> {
	private final String key;
	private final String defaultValue;

	/**
	 * Creates a CLIPropArgedOption.
	 * The parsed value for the option will be stored in a property
	 * named with the same name as the property. The empty string will be used
	 * if the option is not inserted.
	 * @param name	Name of the option
	 * @param description	Option description
	 * @param required	Boolean indicating if the option is required
	 * @param unique	Boolean indicating if the option is unique
	 */
	public CLIPropArgedOption(String name, String description,
			boolean required, boolean unique) {
		this(name, description, name, required, unique);
	}

	/**
	 * Creates a CLIPropArgedOption.
	 * The empty string will be stored in the result
	 * if the option is not inserted.
	 * @param name	Name of the option
	 * @param description	Option description
	 * @param key	Key that will be used to store the parsed value
	 * 				in the resulting Properties
	 * @param required	Boolean indicating if the option is required
	 * @param unique	Boolean indicating if the option is unique
	 */
	public CLIPropArgedOption(String name, String description, String key,
			boolean required, boolean unique) {
		this(name, description, key, "", required, unique);
	}

	/**
	 * Creates a CLIPropArgedOption.
	 * @param name	Name of the option
	 * @param description	Option description
	 * @param key	Key that will be used to store the parsed value
	 * 				in the resulting Properties
	 * @param defaultValue	Value that will be stored
	 * 						if the option is not inserted
	 * @param required	Boolean indicating if the option is required
	 * @param unique	Boolean indicating if the option is unique
	 */
	public CLIPropArgedOption(String name, String description, String key,
			String defaultValue, boolean required, boolean unique) {
		super(name, description, required, unique);
		this.key = key;
		this.defaultValue = defaultValue;
	}

	/**
	 * Gets the key used to store the parsed value
	 * in the resulting Properties
	 * @return	Property key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * Gets the value that will be stored
	 * in the resulting Properties if the option is not inserted
	 * @return Default value
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	@Override
	protected boolean argumentInserted(Iterator<String> args,
			ParseResult<? extends Properties> result, String next) {
		Properties res = result.getResult();
		if (isUnique()) {
			res.setProperty(key, next);
		} else {
			res.setProperty(key, res.getProperty(key, "") + next + "\0");
		}
		return true;
	}

	@Override
	protected boolean argumentNotInserted(
			ParseResult<? extends Properties> result) {
		result.getResult().setProperty(key, defaultValue);
		return false;
	}
}
